#include <signal.h>

#include "include/common.h"
#include "include/storage.h"
#include "include/parser.h"
static ltgsql_storage_engine_t* global_engine = NULL;

void print_banner() {
    printf("=========================================\n");
    printf("           LTGSQL Database System\n");
    printf("               Version %s\n", LTGSQL_VERSION_STRING);
    printf("     Lightweight Transactional Graph SQL\n");
    printf("=========================================\n\n");
}

void print_help() {
    printf("Available commands:\n");
    printf("  CREATE TABLE <name> ...     - Create a new table\n");
    printf("  INSERT INTO <table> ...     - Insert data\n");
    printf("  SELECT ...                 - Query data\n");
    printf("  BEGIN                      - Start transaction\n");
    printf("  COMMIT                  "
        "   - Commit transaction\n");
    printf("  ROLLBACK                   - Rollback transaction\n");
    printf("  EXIT                       - Exit LTGSQL\n");
    printf("  HELP                       - Show this help\n");
    printf("\n");
}

void cleanup() {
    if (global_engine) {
        ltgsql_storage_destroy(global_engine);
        global_engine = NULL;
    }
    ltgsql_log(LTGSQL_LOG_INFO, "LTGSQL shutdown complete");
}

void signal_handler(int sig) {
    ltgsql_log(LTGSQL_LOG_INFO, "Received signal %d, shutting down...", sig);
    cleanup();
    exit(0);
}

void process_command(const char* command) {
    if (strlen(command) == 0) {
        return;
    }

    if (strcasecmp(command, "exit") == 0 || strcasecmp(command, "quit") == 0) {
        cleanup();
        exit(0);
    }

    if (strcasecmp(command, "help") == 0) {
        print_help();
        return;
    }

    // 解析SQL命令
    ltgsql_parsed_stmt* stmt = ltgsql_parse_sql(command);
    if (!stmt) {
        printf("Syntax error or unsupported command\n");
        return;
    }

    // 执行SQL语句
    switch (stmt->type) {
        case LTGSQL_STMT_CREATE_TABLE:
            printf("CREATE TABLE command received\n");
            // TODO: 实现创建表逻辑
            break;

        case LTGSQL_STMT_INSERT:
            printf("INSERT command received\n");
            // TODO: 实现插入逻辑
            break;

        case LTGSQL_STMT_SELECT:
            printf("SELECT command received\n");
            // TODO: 实现查询逻辑
            break;

        case LTGSQL_STMT_BEGIN_TRANSACTION:
            printf("BEGIN TRANSACTION\n");
            // TODO: 实现事务开始
            break;

        case LTGSQL_STMT_COMMIT:
            printf("COMMIT\n");
            // TODO: 实现事务提交
            break;

        case LTGSQL_STMT_ROLLBACK:
            printf("ROLLBACK\n");
            // TODO: 实现事务回滚
            break;

        case LTGSQL_STMT_SHOW_TABLES:
            printf("SHOW TABLES\n");
            // TODO: 实现显示表
            break;

        default:
            printf("Unimplemented command type: %d\n", stmt->type);
    }

    ltgsql_free_parsed_stmt(stmt);
}

int main(int argc, char** argv) {
    // 设置信号处理
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);
    atexit(cleanup);

    print_banner();

    // 初始化存储引擎配置
    ltgsql_storage_config config = {
        .data_dir = "./ltgsql_data",
        .page_size = 8192,
        .buffer_pool_size = 1024 * 1024,
        .use_wal = true,
        .use_compression = false
    };

    // 初始化存储引擎
    global_engine = ltgsql_storage_create(&config);
    if (!global_engine) {
        ltgsql_log(LTGSQL_LOG_FATAL, "Failed to initialize storage engine");
        return 1;
    }

    ltgsql_log(LTGSQL_LOG_INFO, "LTGSQL started successfully");

    // 简单的REPL循环
    char* input = NULL;
    while (1) {
        input = readline("ltgsql> ");
        if (!input) {
            break;
        }

        if (strlen(input) > 0) {
            add_history(input);
            process_command(input);
        }

        free(input);
    }

    return 0;
}